# Flores, Alejandro and Alexander Coppock
# Do Bilinguals Respond More Favorably to Candidate Advertisements in English or in Spanish?
# Forthcoming, Political Communication

rm(list = ls())

# Set your working directory to the replication archive directory

# Install software if necessary
# install.packages(c("stargazer", "tidyverse", "estimatr"))

library(stargazer)
library(tidyverse)
library(estimatr)

study_2 <- read_csv("study_2.csv")
study_3 <- read_csv("study_3.csv")

study_2 <- study_2 %>% mutate(democrat = as.numeric(pid_7 %in% c(1, 2, 3)),
                              republican = as.numeric(pid_7 %in% c(5, 6, 7)))
study_3 <- study_3 %>% mutate(democrat = as.numeric(pid_7 %in% c(1, 2, 3)),
                              republican = as.numeric(pid_7 %in% c(5, 6, 7)))



# Table B.5 ---------------------------------------------------------------

fit_1_b_d <- lm(vela_general ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_1_b_r <- lm(vela_general ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_1_m_d <- lm(vela_general ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_1_m_r <- lm(vela_general ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_2_b_d <- lm(coffman_general ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_2_b_r <- lm(coffman_general ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_coffman)))
fit_2_m_d <- lm(coffman_general ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_2_m_r <- lm(coffman_general ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_coffman)))

het_tab_1_list <- list(fit_1_b_d, fit_1_b_r, fit_1_m_d, fit_1_m_r, fit_2_b_d, fit_2_b_r, fit_2_m_d, fit_2_m_r)

stargazer(het_tab_1_list,
          p = starprep(het_tab_1_list, stat = "p"),
          se = starprep(het_tab_1_list),
          covariate.labels = c("Spanish-language Ad", "Constant (Control Mean)"),
          dep.var.labels = c("Vela: General Election", "Coffman: General Election"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Monolingual", "Bilingual", "Bilingual", "Monolingual", "Monolingual"),
                           c("Party", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "0pt",
          float = FALSE,
          notes = c("HC2 robust standard errors are in parentheses."))


# Table B.6 ---------------------------------------------------------------

fit_3_b_d <- lm(like_vela ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_3_b_r <- lm(like_vela ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_3_m_d <- lm(like_vela ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_3_m_r <- lm(like_vela ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_4_b_d <- lm(like_coffman ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_4_b_r <- lm(like_coffman ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_coffman)))
fit_4_m_d <- lm(like_coffman ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_4_m_r <- lm(like_coffman ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_coffman)))


het_tab_2_list <- list(fit_3_b_d, fit_3_b_r, fit_3_m_d, fit_3_m_r, fit_4_b_d, fit_4_b_r, fit_4_m_d, fit_4_m_r)

stargazer(het_tab_2_list,
          p = starprep(het_tab_2_list, stat = "p"),
          se = starprep(het_tab_2_list),
          covariate.labels = c("Spanish-language Ad", "Constant (Control Mean)"),
          dep.var.labels = c("Like Vela", "Like Coffman"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Monolingual", "Bilingual", "Bilingual", "Monolingual", "Monolingual"),
                           c("Party", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "0pt",
          float = FALSE,
          notes = c("HC2 robust standard errors are in parentheses."))


# Table B.7 ---------------------------------------------------------------

fit_6_b_d <- lm(vela_cares ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_6_b_r <- lm(vela_cares ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_6_m_d <- lm(vela_cares ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_6_m_r <- lm(vela_cares ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_7_b_d <- lm(coffman_cares ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_7_b_r <- lm(coffman_cares ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_coffman)))
fit_7_m_d <- lm(coffman_cares ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_7_m_r <- lm(coffman_cares ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_coffman)))

het_tab_3_list <- list(fit_6_b_d, fit_6_b_r, fit_6_m_d, fit_6_m_r, fit_7_b_d, fit_7_b_r, fit_7_m_d, fit_7_m_r)

stargazer(het_tab_3_list,
          p = starprep(het_tab_3_list, stat = "p"),
          se = starprep(het_tab_3_list),
          covariate.labels = c("Spanish-language Ad", "Constant (Control Mean)"),
          dep.var.labels = c("Vela Cares", "Coffman Cares"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Monolingual", "Bilingual", "Bilingual", "Monolingual", "Monolingual"),
                           c("Party", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "0pt",
          float = FALSE,
          notes = c("HC2 robust standard errors are in parentheses."))


# Table B.8 ---------------------------------------------------------------

fit_9_b_d <- lm(conf_in_vela ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_9_b_r <- lm(conf_in_vela ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_9_m_d <- lm(conf_in_vela ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_vela)))
fit_9_m_r <- lm(conf_in_vela ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_vela)))
fit_10_b_d <- lm(conf_in_coffman ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_10_b_r <- lm(conf_in_coffman ~ Z_ad, data = (study_2 %>% filter(bilingual == 1, republican == 1) %>% mutate(Z_ad = Z_coffman)))
fit_10_m_d <- lm(conf_in_coffman ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, democrat == 1) %>% mutate(Z_ad = Z_coffman)))
fit_10_m_r <- lm(conf_in_coffman ~ Z_ad, data = (study_3 %>% filter(bilingual == 0, republican == 1) %>% mutate(Z_ad = Z_coffman)))


het_tab_4_list <- list(fit_9_b_d, fit_9_b_r, fit_9_m_d, fit_9_m_r, fit_10_b_d, fit_10_b_r, fit_10_m_d, fit_10_m_r)

stargazer(het_tab_4_list,
          p = starprep(het_tab_4_list, stat = "p"),
          se = starprep(het_tab_4_list),
          covariate.labels = c("Spanish-language Ad", "Constant (Control Mean)"),
          dep.var.labels = c("Confidence in Vela", "Confidence in Coffman"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Monolingual", "Bilingual", "Bilingual", "Monolingual", "Monolingual"),
                           c("Party", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans", "Democrats", "Republicans")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "0pt",
          float = FALSE,
          notes = c("HC2 robust standard errors are in parentheses."))
